#!/bin/sh

e2e_test_install() {
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  kubectl create namespace "$CLUSTER_NAMESPACE"
  cat <<EOF | kubectl apply -f -
apiVersion: stackgres.io/v1
kind: SGInstanceProfile
metadata:
  name: size-s
  namespace: $CLUSTER_NAMESPACE
spec:
  cpu: '500m'
  memory: '512Mi'
EOF

  deploy_curl_pod "$CLUSTER_NAMESPACE"
}

e2e_test() {
  run_test "Creating a custom labels directly on the cluster CR" direct_custom_labels

  run_test "Creating a custom labels with the helm chart" helm_custom_labels

  run_test "Creating a custom labels with http api" create_api_custom_labels

  run_test "Getting a custom labels with http api" get_api_custom_labels
}

check_label(){
  local EXPECTED_LABEL_VALUE="$1"

  local ACTUAL_LABEL
  ACTUAL_LABEL="$(wait_until kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o jsonpath="{.spec.template.metadata.labels.test-label}")"

  if assert_string_equal "$EXPECTED_LABEL_VALUE" "$ACTUAL_LABEL"
  then
    success "Custom labels were created"
  else
    fail "Custom labels did not match the expected value"
  fi
}

direct_custom_labels(){
  local RANDOM_VALUE="$(random_string)"
  cat <<EOF | kubectl apply -f -
apiVersion: stackgres.io/v1
kind: SGCluster
metadata:
  name: $CLUSTER_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  instances: 2
  postgres:
    version: "$E2E_POSTGRES_VERSION"
  pods:
    persistentVolume:
      size: '128Mi'
    scheduling:
      nodeSelector:
        $NODE_LABEL_KEY: $NODE_LABEL_VALUE
  sgInstanceProfile: 'size-s'
  nonProductionOptions:
    disableClusterPodAntiAffinity: true
  metadata:
    labels:
      clusterPods:
        test-label: '$RANDOM_VALUE'
EOF

  wait_until check_label "$RANDOM_VALUE"

  kubectl delete sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"
}

helm_custom_labels(){
  local RANDOM_VALUE="$(random_string)"
  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set cluster.metadata.labels.clusterPods.test-label="$RANDOM_VALUE"

  wait_until check_label "$RANDOM_VALUE"

  helm uninstall "$CLUSTER_NAME" --namespace "$CLUSTER_NAMESPACE"
}

get_cluster_json() {
  LABEL_VALUE="$1"

  CLUSTER_JSON="$(kubectl get sgclusters.stackgres.io -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o json \
    | jq 'del(.metadata.creationTimestamp) | del(.metadata.generation)' \
    | jq 'del(.metadata.resourceVersion) | del(.metadata.selfLink) | del(.metadata.uid)' \
    | jq 'del(.apiVersion) | del(.kind)' \
    | jq ".spec.metadata.labels.clusterPods[\"test-label\"]=\"$LABEL_VALUE\"" )"
  echo "$CLUSTER_JSON"
}

assert_http_status_succcess(){
  HTTP_STATUS="$1"
  if [ "$HTTP_STATUS" = "200" ] || [ "$HTTP_STATUS" = "202" ] || [ "$HTTP_STATUS" = "204" ]
  then
    success "Successful request"
  else
    fail "Request not successful"
  fi
}

create_cluster_with_api(){
  HTTP_STATUS="$(run_curl -r "stackgres/sgclusters" -d "$LOG_PATH/stackgres-cluster.json" -e '-X POST -w %{http_code} -o /dev/null')"
  assert_http_status_succcess "$HTTP_STATUS"
}

create_api_custom_labels(){
  cat <<EOF | kubectl apply -f -
apiVersion: stackgres.io/v1
kind: SGCluster
metadata:
  name: $CLUSTER_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  instances: 2
  postgres:
    version: "$E2E_POSTGRES_VERSION"
  pods:
    persistentVolume:
      size: '128Mi'
    scheduling:
      nodeSelector:
        $NODE_LABEL_KEY: $NODE_LABEL_VALUE
  sgInstanceProfile: 'size-s'
  nonProductionOptions:
    disableClusterPodAntiAffinity: true
EOF

  RANDOM_VALUE="$(random_string)"

  CLUSTER_JSON="$(get_cluster_json "$RANDOM_VALUE")"

  echo_raw "$CLUSTER_JSON" > "$LOG_PATH/stackgres-cluster.json"

  kubectl delete sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"

  create_cluster_with_api

  wait_until check_label "$RANDOM_VALUE"

  kubectl delete sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"
}

get_api_custom_labels() {
  local RANDOM_VALUE="$(random_string)"
  cat <<EOF | kubectl apply -f -
apiVersion: stackgres.io/v1
kind: SGCluster
metadata:
  name: $CLUSTER_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  instances: 2
  postgres:
    version: "$E2E_POSTGRES_VERSION"
  pods:
    persistentVolume:
      size: '128Mi'
    scheduling:
      nodeSelector:
        $NODE_LABEL_KEY: $NODE_LABEL_VALUE
  sgInstanceProfile: 'size-s'
  nonProductionOptions:
    disableClusterPodAntiAffinity: true
  metadata:
    labels:
      clusterPods:
        test-label: '$RANDOM_VALUE'
EOF

  local LABEL_VALUE
  LABEL_VALUE="$(run_curl -r "stackgres/namespaces/$CLUSTER_NAMESPACE/sgclusters/$CLUSTER_NAME" | jq '.spec.metadata.labels.clusterPods["test-label"]' -r)"

run_curl -r "stackgres/namespaces/$CLUSTER_NAMESPACE/sgclusters/$CLUSTER_NAME"
  assert_string_equal "$RANDOM_VALUE" "$LABEL_VALUE"
}
